package com.dgut.legalservice.service.impl;

import com.dgut.legalservice.dao.IApproval;
import com.dgut.legalservice.dao.IOrderDao;
import com.dgut.legalservice.domain.Approval;
import com.dgut.legalservice.domain.Order;
import com.dgut.legalservice.service.ApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class ApprovalServiceImpl implements ApprovalService {

    @Autowired
    private IApproval approvalDao;

    @Override
    public List<Approval> findAll() {
        return approvalDao.selectAll();
    }

    @Override
    public int setFirStatus(Approval approval, Integer orderId){
        Example example = new Example(Approval.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId",orderId);
        List<Approval> approvals = approvalDao.selectByExample(example);
        int i;
        if (approvals.size()==0){
            i = approvalDao.insertSelective(approval);
            System.out.println("审核插入："+approval.getId());
        }else {
            Example example1 = new Example(Approval.class);
            i = approvalDao.updateByExampleSelective(approval,example1);
        }
        return i;
    }

    @Override
    public List<Approval> selectById(Integer orderId) {
        Example example = new Example(Approval.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId",orderId);
        List<Approval> approvals = approvalDao.selectByExample(example);

        return approvals;
    }

    @Override
    public Approval findById(Integer id) {
        return approvalDao.selectByPrimaryKey(id);
    }
}
